<template>
  <div class="list" ref="list">
    <div class="header">
      <div :class="tagIndex?'header-ttile ':'header-ttile active'" @click="handleChangeTag(0)"><span>国内城市</span></div>
      <div :class="tagIndex?'header-ttile active':'header-ttile'" @click="handleChangeTag(1)"><span>国际/港澳台</span></div>
    </div>
    <swiper class="swiper" ref="swiper" :options="{}">
      <swiper-slide class="swiper-item">
        <div class="wrapper" ref="insideWrapper">
          <div class="inside">
            <div class="area">
              <div class="title">当前城市</div>
              <div class="content">
                <div class="btn">{{nowCity}}</div>
              </div>
            </div>
            <div class="area">
              <div class="title">热门城市</div>
              <div class="content">
                <div class="btn" v-for="item in hotCities" :key="item.id" @click="handleCityClick(item.name)">{{item.name}}</div>
              </div>
            </div>
            <div class="area" v-for="(item, key) in cities" :key="key">
              <div class="title" :ref="key">{{key}}</div>
              <ul>
                <li class="item" v-for="city in item" :key="city.id" @click="handleCityClick(city.name)">{{city.name}}</li>
              </ul>
            </div>
          </div>
        </div>
        <city-navigation
          :cities="cities"
          :listOffsetTop="listOffsetTop"
          @changeLetter="handleLetterChange"
          @clickLetter="handleLetterClick"
          @touchEnd="handleTouchEnd"
        ></city-navigation>
        <fade-anim>
          <div class="letter" v-show="showLetter">{{letter}}</div>
        </fade-anim>
      </swiper-slide>
      <swiper-slide class="swiper-item">
        <div class="outside">
          <ul class="left">
            <li
              :class="nowClass==index?'active':''"
              @click="handleChangeClass(index, item)"
              v-for="(item, index) in foreignClass"
              :key="index"
            >{{item}}</li>
          </ul>
          <div class="right">
            <div class="item" v-if="classData.recommend">
              <div class="title">热门地点</div>
              <div class="content">
                <div class="imgbox"
                  v-for="item in classData.recommend"
                  :key="item.id"
                  @click="handleCityClick(item.name)"
                >
                  <img :src="item.imgUrl">
                  <span>{{item.name}}</span>
                </div>
              </div>
            </div>
            <div class="item" v-for="(item,index) in classData.normal" :key="index">
              <div class="title">{{item.title}}</div>
              <div class="content">
                <div class="btn"
                  v-for="city in item.data"
                  :key="city.id"
                  @click="handleCityClick(city.name)"
                >{{city.name}}</div>
              </div>
            </div>
          </div>
        </div>
      </swiper-slide>
    </swiper>
  </div>
</template>
<script>
import BScroll from 'better-scroll'
import CityNavigation from './Navigation'
import FadeAnim from '../../common/FadeAnim'
import { setTimeout } from 'timers';
export default {
  name: "CityList",
  props: {
    hotCities: Array,
    cities: Object,
    foreignCities: Object
  },
  components:{
    CityNavigation,
    FadeAnim
  },
  data() {
    return { 
      tagIndex: 0,
      letter: '',
      showLetter: false,
      listOffsetTop: 0,
      nowClass: 0,
      classData: {}
    }
  },
  computed: {
    nowCity (){
      return this.$store.state.city
    },
    swiper(){
      return this.$refs.swiper.swiper
    },
    foreignClass() {
      let array = []
      for(let i in this.foreignCities){
        array.push(i)
      }
      return array
    },
  },
  watch:{
    // 监听当国外数据存在时，设置classData初始化数据
    foreignCities(val) {
      this.classData = val['推荐']
    }
  },
  methods: {
    handleCityClick(nowCity) {
      this.$store.dispatch('changeCity', nowCity)
      this.$router.go(-1)
    },
    handleChangeTag(index) {
      this.swiper.slideTo(index)
      this.tagIndex = index;
    },
    // 点击导航
    handleLetterClick(letter) {
      this.showLetter = true;
      setTimeout(()=>{
        this.showLetter = false
      }, 1000)
      this.letter = letter;
      this.scroll.scrollToElement(this.$refs[letter][0])
    },
    // 触摸滑动选择导航
    handleLetterChange(letter) {
      this.showLetter = true;
      this.letter = letter;
      this.scroll.scrollToElement(this.$refs[letter][0])
    },
    // 触摸滑动结束
    handleTouchEnd(){
      this.showLetter = false
    },
    handleChangeClass(index, item) {
      this.nowClass = index;
      this.classData = this.foreignCities[item]
    }
  },
  mounted() {
    // better-scroll初始化
    this.scroll = new BScroll(this.$refs.insideWrapper, {
      click: true
    })
    // 获取list组件到顶部的距离
   this.listOffsetTop = this.$refs['list'].offsetTop;
   // swiper改变
   this.swiper.on('slideChange', ()=>{
     this.tagIndex = this.swiper.activeIndex;
   })

   // classData 初始化数据
   //this.classData = this.foreignCities['推荐']
  }
}
</script>
<style lang="scss" scoped>
  @import '../../../assets/base.scss';
  .list{
    position: absolute;
    top: 160px;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: #f5f5f5;
    .header{
      display: flex;
      height: 60px;
      background-color: #fff;
      .header-ttile{
        font-size: 28px;
        flex: 1;
        text-align: center;
        line-height: 60px;
        &.active{
          color: $headerColor;
          span{
            padding: 12px 0;
            border-bottom: 4px solid $headerColor;
          }
        }
      }
    }
    .swiper{
      height: calc(100% - 60px);
      .wrapper{
        position: absolute;
        top: 0;
        bottom: 0;
        .inside{
          .area{
            .title{
              padding-left: 20px;
              font-size: 24px;
              line-height: 50px;
              position: sticky;
              top: 0;
              z-index: 5;;
            }
            .content{
              display: flex;
              flex-wrap: wrap;
              padding: 20px 40px 20px 20px;
              font-size: 30px;
              background-color: #fff;
              .btn{
                margin-right: 20px;
                margin-top: 15px;
                padding: 20px 40px;
                border: 1px solid #ddd;
                &:nth-child(1),&:nth-child(2),&:nth-child(3),&:nth-child(4){
                  margin-top: 0;
                }
              }
            }
            ul{
              padding-left: 20px;
              padding-right: 40px;
              background-color: #fff;
              .item{
                border-top: 1px solid #f0f0f0;
                line-height: 70px;
                font-size: 30px;
                &:first-child{
                  border-top: none;
                }
              }
            }
          }
        }
      }
    }
  }
  .letter{
    z-index: 5;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    width: 80px;
    height: 80px;
    text-align: center;
    line-height: 80px;
    font-size: 40px;
    background-color: $headerColor;
    color: #fff;
    border-radius: 50%;
  }
  .outside{
    display: flex;
    height: 100%;
    border-top: 1px solid #ddd;
    background-color: #fff;
    .left{
      flex-shrink: 0;
      height: 100%;
      overflow-y: scroll;
      width: 200px;
      background-color: #f5f5f5;
      li{
        line-height: 100px;
        text-align: center;
        color: #888;
        font-size: 30px;
        &:first-child{
          border-top: none;
        }
        &.active{
          background-color: #fff;
          color: $headerColor;
        }
      }
    }
    .right{
      overflow-y: scroll;
      flex: 1;
      padding: 30px 20px 60px 20px;
      .item{
        margin-top: 20px;
        .title{
          line-height: 60px;
          color: #aaa;
        }
        .content{
          display: flex;
          flex-wrap: wrap;
          .imgbox{
            border-radius: 10px;
            overflow: hidden;
            margin-right: 20px;
            margin-top: 20px;
            position: relative;
            width: 230px;
            height: 140px;
            img{
              width: 100%;
              height: 100%;
            }
            span{
              position: absolute;
              top: 0;
              right: 0;
              left: 0;
              bottom: 0;
              margin: auto;
              background-color: rgba(0, 0, 0, .5);
              text-align: center;
              line-height: 140px;
              font-size: 30px;
              color: #fff;
            }
          }
          .btn{
            margin-bottom: 20px;
            margin-right: 20px;
            padding: 20px 40px;
            border: 1px solid #ddd;
            font-size: 28px;
          }
        }
      }
    }
  }
  
</style>
